package com.evan.review.architecture.mvvm.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;

import com.evan.review.architecture.mvvm.model.Note;
import com.evan.review.architecture.mvvm.repository.NoteRepository;

import java.util.List;

/**
 * 笔记ViewModel
 * MVVM架构中的ViewModel层
 * 负责准备和管理UI数据，处理用户交互，并通知视图状态变化
 */
public class NoteViewModel extends AndroidViewModel {
    
    private final NoteRepository repository;
    private final LiveData<List<Note>> allNotes;
    private final LiveData<List<Note>> archivedNotes;
    private final LiveData<List<Note>> highPriorityNotes;
    
    // 用于搜索功能
    private final MutableLiveData<String> searchQuery = new MutableLiveData<>();
    private final LiveData<List<Note>> searchResults;
    
    // 用于分类过滤
    private final MutableLiveData<String> categoryFilter = new MutableLiveData<>();
    private final LiveData<List<Note>> notesByCategory;
    
    public NoteViewModel(@NonNull Application application) {
        super(application);
        repository = new NoteRepository(application);
        allNotes = repository.getAllNotes();
        archivedNotes = repository.getArchivedNotes();
        highPriorityNotes = repository.getHighPriorityNotes();
        
        // 当搜索查询变化时，自动更新搜索结果
        searchResults = Transformations.switchMap(searchQuery, query -> 
                repository.searchNotes(query));
        
        // 当分类过滤变化时，自动更新分类结果
        notesByCategory = Transformations.switchMap(categoryFilter, category ->
                repository.getNotesByCategory(category));
    }
    
    // 数据操作方法
    
    public void insert(Note note) {
        repository.insert(note);
    }
    
    public void update(Note note) {
        note.setUpdatedAt(System.currentTimeMillis()); // 更新时间戳
        repository.update(note);
    }
    
    public void delete(Note note) {
        repository.delete(note);
    }
    
    public void deleteAllNotes() {
        repository.deleteAllNotes();
    }
    
    // 归档/取消归档笔记
    public void toggleArchiveStatus(Note note) {
        note.setArchived(!note.isArchived());
        note.setUpdatedAt(System.currentTimeMillis());
        repository.update(note);
    }
    
    // 更新笔记优先级
    public void updatePriority(Note note, int priority) {
        note.setPriority(priority);
        note.setUpdatedAt(System.currentTimeMillis());
        repository.update(note);
    }
    
    // 获取数据的方法
    
    public LiveData<List<Note>> getAllNotes() {
        return allNotes;
    }
    
    public LiveData<List<Note>> getArchivedNotes() {
        return archivedNotes;
    }
    
    public LiveData<List<Note>> getHighPriorityNotes() {
        return highPriorityNotes;
    }
    
    // 搜索相关方法
    
    public void setSearchQuery(String query) {
        searchQuery.setValue(query);
    }
    
    public LiveData<List<Note>> getSearchResults() {
        return searchResults;
    }
    
    // 分类过滤相关方法
    
    public void setCategoryFilter(String category) {
        categoryFilter.setValue(category);
    }
    
    public LiveData<List<Note>> getNotesByCategory() {
        return notesByCategory;
    }
} 